define(['jquery','api','user','util','text!pages/fund/cash.html','Vue'],function($,api,user,util){
	var vm,
	   statusHash={},
	   lang={
	      0:'我要提现',
	      2:'微信授权',
	      1:'去绑银行卡'
	   },
	   controller = {
	   init:function(id){
	   	   if(!user.checkLogin())return;
		   api.initFooter(3);
		   api.setTitle('提现');
		   if(!vm){
		     this.render();
		   }else{
		     vm.show=true;
		   }
		   this.initChannel();
		   this.loadAllowFund();
	   },
	   render:function(){
	   	   $('#pages').append('<div id="cash"></div>');
	       require(['text!pages/fund/cash.html','Vue'],function(tpl,Vue){
	            vm=new Vue({
	               el:'#cash',
	               data:{
	               	  show:true,
	               	  allowfunds:'--',
	               	  outChannel:'',
	               	  canCash:false,
	               	  outMoney:'',
	               	  pwd:'',
	               	  channels:false,
	               	  buttonValue:''
	               },
	               watch:{
	                  outChannel:function(v){
	                  	 var status=statusHash[v],
	                  	     str=lang[status];
	                  	 vm.canCash=status==0;
	                  	 vm.buttonValue=str;
	                  }
	               },
	               filters:{
	                   priceFormat:function(v){
	                      if(isNaN(v)){return v='--'}; // 20161209dyx
	                      return util.priceFormat(2,v);
	                   }
		    	   },
	               methods:{
	               	  moneyChange:function(v,event){
		           	     var s,
		           	  	     str,
		           	  	     d=$(event.target),
		           	  	     c=d.val(),
		           	  	     a=this.allowfunds/100,
		           	  	     i;
		           	  	 if(!v)return;
		           	  	 str=c.replace(/[^\d\.]/g, "");
		           	  	 i=str.indexOf('.');
		           	  	 if(str&&i>-1){
		           	  	 	 s=str.split('.')[1].length;
		           	  	     if(s>2){
		           	  	         c=str.substr(0,i)+str.substr(i,i+2);
		           	  	     }
		           	  	 }
		           	  	 if(a!='--'&&Number(c)>a){
		           	  	     c=a.toFixed(2);
		           	  	 }
		           	  	 if(v!=Number(c)||str!=c)d.val(c);
			          },
	                  toCash:function(){
	                     var data={
		                	   amount:this.outMoney*100,
		                	   channel:this.outChannel,
		                	   moneypwd:this.pwd
		                	},status=statusHash[this.outChannel];
		                	if(this.canCash){
		                		if(data.amount==''){
		                		   util.msg('输入金额格式不正确！');
		                		   return;
		                		}else if(data.moneypwd==''){
		                		   util.msg('请输入密码！');
		                		   return;
		                		}else if(parseInt(data.amount)>this.allowfunds){
		                		   util.msg('提现金额不能大于账户余额！');
		                		   return
		                		}
		                	    api.cash(data,function(rs){
		                	        if(rs.retcode=='0'){
		                	            util.msg('提现申请成功！');
		                	            setTimeout(function(){history.go(-1)},500)
		                	        }else{
		                	            util.dealErrorCode(rs.retcode);
		                	        }
		                	    });
		                	}else if(status==1){
		                		location.hash='#/fund/bind/'+this.outChannel;
		                	}else if(status==2){
		                	
		                	}
	                  }
	               },
	               template:tpl
	            })
	       })
	   },
	   loadAllowFund:function(){
	      api.allowFund(function(rs){
	          if(rs.retcode=='0'){
	               vm.allowfunds=rs.allowoutfunds
	          }
	      })
	   },
	   initChannel:function(){
	   	   var userchannels,
	   	       channels,
	   	       channelHash={},
	   	       channel,
	   	       channelid,
	   	       userchannel,
	   	       temp=[],
	   	       cb=function(){
	   	       	  for(var i=0;i<userchannels.length;i++){
	   	       	  	   channel=userchannels[i];
	   	       	       channelHash[channel.channelid]=channel;
	   	       	  }
	   	       	  for(var i=0;i<channels.length;i++){
	   	       	  	   channel=channels[i];
	   	       	       if(+channel.outmode>50){
		   	       	  	  channelid=channel.channelid;
		   	       	  	  userchannel=channelHash[channelid];
		   	       	  	  if(userchannel){
		   	       	  	  	  channel["bankid"]=userchannel.bankid;
	                      	  channel.bankaccount=userchannel.bankaccount;
		   	       	  	  }
//	                      channel.bankaccountname=userchannel.bankaccountname;
	                      statusHash[channelid]=channel.bankaccount?0:1;
	   	       	       	  temp.push(channel);
	   	       	       }
	   	       	  }
	           	  vm.channels=temp;
	   	      }
	   	       	  
	   	   api.channels(function(rs){
	           if(rs.retcode=='0'){
	           	  channels=rs.data;
	           	  if(userchannels)cb();
	           }
	       });
	   	   api.userchannels(function(rs){
	           if(rs.retcode=='0'){
	           	  userchannels=rs.data?rs.data:[];
	           	  if(channels)cb();
	           }
	       });
	   },
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	   }
	};
    return controller;
})